package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import com.itheima.tlias84.mapper.DeptMapper;
import com.itheima.tlias84.po.InsertDeptParam;
import com.itheima.tlias84.service.DeptService;
import com.itheima.tlias84.service.impl.DeptServiceImpl;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/depts")

@CrossOrigin
/**
 * @RequestMapping Target -> 类上 ,方法上
 *                  类上 -> 提取一个controller中的重复路径
 *                  方法上 -> 定义该接口的请求方法的 被对应的GetMapping ..PostMapping等替代了
 */
public class DeptController {
    /**
     * 属性注入优先使用@Resource
     */
//    @Autowired
//    DeptService service;
    @Resource
    DeptService service;


    @GetMapping
    // @RequestMapping(method = RequestMethod.GET)
    public ResultUtil selectAll() {
        List<SelectAllDeptDTO> depts = service.selectAll();
        return ResultUtil.success(depts);
    }

    /**
     * get/delete 请求参数一般拼接到url后面
     * post/put 请求参数一般放在请求体里面,且以JSON格式进行传输
     *
     * @param
     * @return
     * @RequestBody 作用一就是从http请求体中读传输的数据
     * 作用二 把JSON字符串自动变成对象
     */
    @PostMapping
    // @RequestMapping(method = RequestMethod.POST)
    public ResultUtil insertDept(@RequestBody InsertDeptParam param) {
        //把JSON字符串变成对象
//        log.info("读取请求体中的数据{}", param.getName());
        int i = service.insertDept(param);
        return i == 0 ? ResultUtil.fail("新增失败") : ResultUtil.success("新增成功");
    }

    /**
     * @param id
     * @return
     * @RequestParam 注解的作用 :
     * 1. 根据指定的K,获取Http URL中的值  value/name属性
     * 2.可以指定是否必传参数 ,如果指定必传 ,前端没传相应参数 -> 400   required
     * 3.给参数指定默认值  defaultValue 默认值属性
     */
    @DeleteMapping
    public ResultUtil deleteById(@RequestParam("id") Integer id) {
        log.info("删除id{}", id);
        int i = service.deleteById(id);
        return i == 0 ? ResultUtil.fail() : ResultUtil.success();
    }

    /**
     * @param id
     * @return
     * @PathVariable 注解作用
     * 获取URI以/分割的路径变量
     */
    @GetMapping("/{id}")
    public ResultUtil selectDeptById(@PathVariable Integer id) {
        log.info("使用PathVariable注解获取{}", id);
        SelectAllDeptDTO dto = service.selectDeptById(id);
        return ResultUtil.success(dto);
    }

    @PutMapping
    public ResultUtil updateDeptById(@RequestBody Dept dept) {
        log.info("名字{}", dept.getName());
        return service.updateDeptById(dept);
    }
    @DeleteMapping("batchDelete")
    //public ResultUtil batchDelete(@RequestParam("ids") int[] ids) {
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids,HttpServletRequest request) {
        String[] idsD = request.getParameterValues("ids");
        log.info("通过servlet取重复参数{}",idsD);
        log.info("接收到的id={}",ids);
        service.batchDelete(ids);
        return ResultUtil.success();
    }

}
